package com.bit.demo.springbookdemo.controller;

import com.bit.demo.springbookdemo.constant.Constant;
import com.bit.demo.springbookdemo.model.UserInfo;
import com.bit.demo.springbookdemo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        //1.判断用户是否为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //2.判断用户是否正确
//        if("admin".equals(userName)&&"admin".equals(password)){
//            session.setAttribute("userName",userName);
//            return true;
//        }
        UserInfo userInfo = userInfoService.queryUserByName(userName);
        if(userInfo==null){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            //3.登录成功，将用户信息放入session
            userInfo.setPassword("");
            session.setAttribute(Constant.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
    }
}
